package com.zhida.smartcontrol.pagectrl.projectdetail.zonemanager;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.yanzhenjie.kalle.Params;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;
import com.zhida.smartcontrol.pagectrl.projectdetail.GroupBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: tanjianpei
 * date: 2019/10/11 011 11:47
 * description:
 */
public class ZoneManagerPresenter extends BasePresenterImpl<ZoneManagerContract.View> implements ZoneManagerContract.Presenter {

    @Override
    public void requestGroup(String projectId) {
        Params params = Params.newBuilder()
                .add("projectBaseId", projectId)
                .build();
        mKalleCenter.get(Api.GROUP_LIST, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                List<GroupBean> groupList = JSON.parseArray(result, GroupBean.class);
                mView.showGroup(groupList);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void requestDevice(String switchType, String groupId, boolean showLoading) {
        if (TextUtils.isEmpty(switchType) || TextUtils.isEmpty(groupId)) {
            return;
        }
        Params params = Params.newBuilder()
                .add("switchType", switchType)
                .add("groupId", groupId)
                .build();
        mKalleCenter.get(Api.DEVICE_LIST, params, showLoading, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                List<LampBean> lampBeans = JSON.parseArray(result, LampBean.class);
                mView.showLamp(lampBeans);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void requestDelete(String id) {
        Params params = Params.newBuilder()
                .add("id", id)
                .build();
        mKalleCenter.delete(Api.DEVICE_DELETE, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void runningModel(List<LampBean> codeList, int type) {
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", type);
        mKalleCenter.postJson(Api.RUNNING_MODEL, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void switchRepair(List<LampBean> codeList, int type) {
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", type);
        mKalleCenter.postJson(Api.SWITCH_REPAIR, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void resetSwitch(List<LampBean> codeList) {
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        mKalleCenter.postJson(Api.RESET_SWITCH, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
}
